package code.snippet.ajax

import scala.xml.{Text, NodeSeq}
import net.liftweb.http.{Templates, S, SHtml}
import net.liftweb.http.js.JsCmds.SetHtml
import code.snippet.bootstrap.currentPage

case class PageLink2Ajax(target: String = "page-content") extends (NodeSeq => NodeSeq) {
  def apply(src: NodeSeq): NodeSeq = {
    val href = (src \ "@href").text
    var partial = href.replace(".html", "").split('/').filter(_.nonEmpty).toList
    SHtml.a(() => {
      currentPage.set(href)
      SetHtml(S.attr("target") openOr target, Templates(partial).get)
    }, Text(src.head.text))
  }

}
